#include <string.h>
#include "event.h"
#include "list.h"

#if 0
LIST_HEAD(event_list);

int wait(void)
{
    sleep(1);

    return 1;
}

void event_pull(void)
{
    struct list_head *i;
    while(wait()){
        list_for_each(i,&event_list){
            struct _event *e = (void *)i;
            if(e->event_count && e->event_count()){
                e->event_cb && e->event_cb(e->data,e->data_len);
            }
        }
    }
}

void event_init(void)
{

}

void event_add(struct list_head *h)
{
    list_add(h,&event_list);
}
#endif

SLIST_HEAD(s_poll_cbs, cb_info) s_poll_cbs = SLIST_HEAD_INITIALIZER(s_poll_cbs);

void event_add_poll_cb(poll_cb_t cb, void *cb_arg)
{
    struct cb_info *ci = (struct cb_info *) calloc(1, sizeof(*ci));
    ci->cb = cb;
    ci->cb_arg = cb_arg;
    SLIST_INSERT_HEAD(&s_poll_cbs, ci, poll_cbs);
}

void event_add_poll_cb_ex(struct cb_info *ci,poll_cb_t cb, void *cb_arg)
{
    ci->cb = cb;
    ci->cb_arg = cb_arg;
    SLIST_INSERT_HEAD(&s_poll_cbs, ci, poll_cbs);
}

int event_poll(int ms) 
{
//   {
    struct cb_info *ci, *cit;
    SLIST_FOREACH_SAFE(ci, &s_poll_cbs, poll_cbs, cit) {
      ci->cb(ci->cb_arg);
    }
//   }
  usleep(10*1000);

  return 0;
}